package actionbean;

import java.io.File;
import java.io.IOException;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.FileBean;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.StripesRequestWrapper;
import util.JsonUtil;
import util.UnzipUtil;
import controller.RoundCtrl;
import controller.webservice.bootstrap.BootstrapCtrl;
import exception.InputValidationErrors;

/**
 * @author jaslin.wee.2012, sheryl.tay.2012
 * 
 */
@UrlBinding(BaseActionBean.WEBSERVICE_PREFIX + "bootstrap")
public class BootstrapActionBean extends BaseActionBean {

	private static final String BOOTSTRAP_FILE_FIELD = "bootstrap-file";
	private FileBean zipFile;

	@DefaultHandler
	public Resolution bootstrap() {
		// Set standard HTTP/1.1 no-cache headers.
		getContext().getResponse().setHeader("Cache-Control", "private, no-store, no-cache, must-revalidate");

		// Set standard HTTP/1.0 no-cache header.
		getContext().getResponse().setHeader("Pragma", "no-cache");
		
		
		try {
			// obtain the stripes request wrapper
			StripesRequestWrapper stripesRequestWrapper = StripesRequestWrapper
					.findStripesWrapper(getContext().getRequest());

			// check if the request is multipart
			if (!stripesRequestWrapper.isMultipart()) {
				return new StreamingResolution("text",
						"ahem, erm! expecting multipart request but received non-multipart request.");
			}

			// obtain the bootstrap-file stuff from the request
			zipFile = stripesRequestWrapper
					.getFileParameterValue(BOOTSTRAP_FILE_FIELD);

			// check if the file field or uploaded file exists
			if (zipFile == null) {
				return new StreamingResolution("text",
						"oh no! either the expected file field \""
								+ BOOTSTRAP_FILE_FIELD
								+ "\" was not found or no file was uploaded.");
			}

			// initialize paths to save file
			String realPathToDataDirectory = getContext().getServletContext()
					.getRealPath(BootstrapCtrl.BOOTSTRAP_DATA_DIRECTORY) + "/";
			String realPathToDataZipFile = realPathToDataDirectory
					+ BootstrapCtrl.BOOTSTRAP_DATA_ZIP_FILE;

			// save file on server
			zipFile.save(new File(realPathToDataZipFile));

			// unzip file
			UnzipUtil.extractZipFile(realPathToDataZipFile,
					realPathToDataDirectory);

			// bootstrap
			BootstrapCtrl bootstrapCtrl = new BootstrapCtrl(
					realPathToDataDirectory);
			bootstrapCtrl.startBootstrap();

			// start round #1
			RoundCtrl roundCtrl = new RoundCtrl();
			try {
				roundCtrl.startRound();

				// return results of bootstrap
				return new StreamingResolution("application/json",
						bootstrapCtrl.dumpResult().toString(4));

			} catch (InputValidationErrors e) {
				return new StreamingResolution("application/json", JsonUtil
						.newJsonErrorResponse(e.toJsonArray()).toString(4));
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return new StreamingResolution(
				"text",
				"oh no! something went wrong while trying to save zip file, unzip it or read the CSV file.");
	}
}
